﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using SYcwpb.Helpers;
using System.Collections.ObjectModel;
using SYcwpb.Models;
using SYcwpb.Commands;
using System.Windows.Input;
using SYcwpb.Access;
using System.Windows.Threading;
using System.Threading;
using SYcwpb.Views;
using System.Windows;
using System.Collections.Specialized;
using SYcwpb.ViewModels.DataProvider;

namespace SYcwpb.ViewModels
{
    class DriverAttendanceOrderAdjustViewModel:ViewModelBase
    {
        private DriverAttendanceOrderAdjustView _window;
        private DriverAttendanceOrderAdjustAccess _access = new DriverAttendanceOrderAdjustAccess();

        //private ObservableCollection<DriverAttendanceOrderAdjustModel> _dutyInfoCollections ;
        AttendanceObjects _attendances;
        private List<string> _groupList;
        private string _group;
        private string _grpHeader;

        //public ObservableCollection<DriverAttendanceOrderAdjustModel> DutyInfoCollections
        //{
        //    get{
        //        if (null == _dutyInfoCollections)
        //            _dutyInfoCollections = _access.GetDriversByGroup(null);

        //        return _dutyInfoCollections;
        //    }
        //}

        public AttendanceObjects Attendances
        {
            get
            {
                if (null == _attendances)
                    GetAttendances();

                return _attendances;
            }
        }

        public List<string> GroupList
        {
            get
            {
                if (null == _groupList)
                    _groupList = new List<string>() { "早班", "白班", "夜班" };
                
                return _groupList;
            }
        }

        public string GrpHeader
        {
            get
            {
                return null;
            }
            set
            {
                ;
            }
        }

        public string Group
        {
            get { return _group; }
            set
            {
                if (value == _group)
                    return;

                _group = value;
                OnPropertyChanged("Group");
            }
        }

        public DriverAttendanceOrderAdjustViewModel(DriverAttendanceOrderAdjustView window)
        {
            _window = window;
            
            //DutyInfoCollections.CollectionChanged += new NotifyCollectionChangedEventHandler(DutyInfoCollections_CollectionChanged);
        }

        public void GetAttendances()
        {
            if (null ==_attendances )
            {
                _attendances = new AttendanceObjects();
                _attendances.itemEndEdit += new ItemEndEditEventHandler(attendances_itemEndEdit);
                _attendances.CollectionChanged += new NotifyCollectionChangedEventHandler(attendances_CollectionChanged);
            }
            _attendances.Clear();
            ObservableCollection<DriverAttendanceOrderAdjustModel> driverModels = _access.GetDriversByGroup(Group);
            foreach (DriverAttendanceOrderAdjustModel model in driverModels)
            {
                _attendances.Add(new AttendanceObject(model));
            }

            OnPropertyChanged("Attendances");
        }

        void attendances_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            //throw new NotImplementedException();
        }

        void attendances_itemEndEdit(System.ComponentModel.IEditableObject sender)
        {
            //throw new NotImplementedException();
            AttendanceObject item = sender as AttendanceObject;

            MessageBoxHelper.ShowTips(string.Format("ID == {0}", item.ID));
        }

        public void DeleItem(DriverAttendanceOrderAdjustModel model)
        {
            //DutyInfoCollections.Remove(model);
        }

        //void DutyInfoCollections_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        //{
        //    //throw new NotImplementedException();
        //    if (e.Action != NotifyCollectionChangedAction.Remove)
        //    {
        //        foreach (object item in e.NewItems)
        //        {
        //            DriverAttendanceOrderAdjustModel model = item as DriverAttendanceOrderAdjustModel;

        //            MessageBoxHelper.ShowTips(string.Format("ID == {0}",model.ID));
        //        }
        //    }
        //}


    }
}
